/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;
using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	public class GS_EndGame : GameState
	{
		// Surfaces
		Surface _sfFrame;
		Surface[] _sfAvatars;
		
		// Labels
		Label _lblTitle;
		Label[] _lblPlayers;
		Label[] _lblScores;
		Label _lblMessage;
		
		// Elements Location
		Rectangle _rctFrame;
		
		bool[] _bWinners;
		
		Rectangle[] _rctAvatars;
				
		public GS_EndGame()
		{
			// Initializing the labels
			// Title
			_lblTitle = new Label(LanguageManager.Get("END_Title"), 36, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			_lblTitle.Location = new Point(0,15);
			// Message
			_lblMessage = new Label(LanguageManager.Get("END_Message"), 24, Color.AntiqueWhite, TextAlignment.TopMiddle);
			_lblMessage.AutoHeight = true;
			_lblMessage.Location = new Point(0, 450);
			
			// Initializing Players' informations
			_bWinners = new bool[PlayerManager.Count];
			_sfAvatars = new Surface[PlayerManager.Count];
			_rctAvatars = new Rectangle[PlayerManager.Count];
			_lblPlayers = new Label[PlayerManager.Count];
			_lblScores = new Label[PlayerManager.Count];
			
			int winnerScore = 0;
			for(int i=0; i<PlayerManager.Count; i++)
			{
				// Name Label
				_lblPlayers[i] = new Label(PlayerManager.GetName(i), 30, Color.Orange, TextAlignment.TopLeft);
				_lblPlayers[i].Location = new Point(120, 80+i*100);
				_lblPlayers[i].AutoHeight = true;
				_lblPlayers[i].AutoWidth = true;
				// Score Label
				_lblScores[i] = new Label(PlayerManager.GetScore(i).ToString() + " ", 26, Color.Orange, TextAlignment.TopLeft);
				_lblScores[i].Text += (Math.Abs(PlayerManager.GetScore(i))>1) ? LanguageManager.Get("END_MorePoints") : LanguageManager.Get("END_OnePoint");
				_lblScores[i].Location = new Point(150, 120+i*100);
				_lblScores[i].AutoHeight = true;
				_lblScores[i].AutoWidth = true;
				// Avatar
				_rctAvatars[i] = new Rectangle(new Point(30, 80+i*100), new Size(80,80));
				_sfAvatars[i] = SurfaceManager.GetSurface("avatars/"+PlayerManager.GetAvatar(i)).CreateStretchedSurface(_rctAvatars[i].Size);
				
				// Finding the winner(s)
				if (PlayerManager.GetScore(i)==winnerScore)
					_bWinners[i] = true;
				else if (PlayerManager.GetScore(i)>winnerScore)
				{
					winnerScore = PlayerManager.GetScore(i);
					_bWinners[i] = true;
					for (int j=0; j<i; j++)
						_bWinners[j]=false;
				}	
			}
		}
		
		public void Draw()
		{
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
		}
		
		
		public void CleanUp()
		{
			Events.Remove();
		}
		
		public void HandleEvents()
		{
			if (Events.Poll())
			{
				KeyboardState state = new KeyboardState();
				if (state[Key.Escape])
					GameStateManager.Quit();
				else if (state[Key.Space])
				{
					while(PlayerManager.Count>0)
						PlayerManager.RemovePlayer(0);
					GameStateManager.PopState();
					GameStateManager.ChangeState(new GS_MenuMain());
				}
			}
		}
		
		public void Update()
		{
			
		}
		
		public void Init()
		{
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(150,50), _sfFrame.Size);
			
			// Initializing the title
			_lblTitle.Width = _rctFrame.Width;
			_lblTitle.AutoHeight = true;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			
			// Initializing the player's Informations
			for(int i=0; i<PlayerManager.Count; i++)
			{
				// If it's a winner, it's not the same background color
				Color textColor = Color.Orange;
				if (_bWinners[i])
				{
					Surface sTemp = new Surface(new Size(_sfFrame.Width-40, 90));
					sTemp.Fill(Color.Gold);
					_sfFrame.Blit(sTemp, new Point(20, 75+100*i));
					textColor = Color.Black;
				}
				
				_lblPlayers[i].TextColor = textColor;
				_lblPlayers[i].Init();
				_sfFrame.Blit(_lblPlayers[i].Surface, _lblPlayers[i].Location);
				_lblScores[i].TextColor = textColor;
				_lblScores[i].Init();
				_sfFrame.Blit(_lblScores[i].Surface, _lblScores[i].Location);
				_sfFrame.Blit(_sfAvatars[i], _rctAvatars[i].Location);
			}
			
			// Initializing the message
			_lblMessage.Width = _rctFrame.Width;
			_lblMessage.Init();
			_sfFrame.Blit(_lblMessage.Surface, _lblMessage.Location);
		}
	}
}
